package com.hanqian.kepler.core.service.core.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.hanqian.kepler.common.base.dao.BaseDao;
import com.hanqian.kepler.common.base.service.BaseServiceImpl;
import com.hanqian.kepler.common.bean.result.AjaxResult;
import com.hanqian.kepler.common.enums.BaseEnumManager;
import com.hanqian.kepler.common.jpa.specification.Rule;
import com.hanqian.kepler.common.jpa.specification.SpecificationFactory;
import com.hanqian.kepler.common.utils.text.Convert;
import com.hanqian.kepler.core.dao.primary.core.TargetDescZanDao;
import com.hanqian.kepler.core.entity.primary.core.TargetDesc;
import com.hanqian.kepler.core.entity.primary.core.TargetDescZan;
import com.hanqian.kepler.core.service.core.TargetDescService;
import com.hanqian.kepler.core.service.core.TargetDescZanService;
import com.hanqian.kepler.flow.entity.User;
import com.hanqian.kepler.flow.enums.FlowEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2021/1/11 。
 * ============================================================================
 */
@Service
public class TargetDescZanServiceImpl extends BaseServiceImpl<TargetDescZan, String> implements TargetDescZanService {

	@Autowired
	private TargetDescZanDao targetDescZanDao;
	@Autowired
	private TargetDescService targetDescService;

	@Override
	public BaseDao<TargetDescZan, String> getBaseDao() {
		return targetDescZanDao;
	}

	@Override
	public List<TargetDescZan> getZanListByTargetDesc(TargetDesc targetDesc) {
		if(targetDesc == null) return new ArrayList<>();
		List<Rule> rules = new ArrayList<>();
		rules.add(Rule.eq("state", BaseEnumManager.StateEnum.Enable));
		rules.add(Rule.eq("targetDesc", targetDesc));
		rules.add(Rule.eq("versionIndex", targetDesc.getVersionIndex()));

		Sort sort = new Sort(Sort.Direction.DESC, "createTime");

		return findAll(SpecificationFactory.where(rules), sort);
	}

	@Override
	public boolean checkIfZaned(TargetDesc targetDesc, String userId) {
		if(targetDesc == null || StrUtil.isBlank(userId)){
			return true;
		}

		List<String> zanIds = this.findZanDescIds(targetDesc.getTarget().getId(), userId);
		return CollUtil.contains(zanIds, targetDesc.getId());
	}

	@Override
	public AjaxResult zan(TargetDesc targetDesc, User user) {
		if(targetDesc!=null && user!=null){
			TargetDescZan targetDescZan = new TargetDescZan();
			targetDescZan.setTargetDesc(targetDesc);
			targetDescZan.setVersionIndex(targetDesc.getVersionIndex());
			targetDescZan.setCreator(user);
			targetDescZan = save(targetDescZan);

			if(StrUtil.isNotBlank(targetDescZan.getId())){
				targetDesc.setZanCount(this.getZanCount(targetDesc));
				targetDescService.save(targetDesc);
				return AjaxResult.success("点赞成功");
			}

		}
		return AjaxResult.error("点赞失败");
	}

	@Override
	public List<String> findZanDescIds(String targetId, String userId) {
		return targetDescZanDao.findZanDescIds(targetId, userId);
	}

	@Override
	public int getZanCount(TargetDesc targetDesc) {
		List<Rule> rules = new ArrayList<>();
		rules.add(Rule.eq("state", BaseEnumManager.StateEnum.Enable));
		rules.add(Rule.eq("targetDesc", targetDesc));
		rules.add(Rule.eq("versionIndex", targetDesc.getVersionIndex()));
		return Convert.toInt(count(SpecificationFactory.where(rules)));
	}
}
